<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        div {
            width: 100px;
            height: 100px;
            margin-bottom: 20px;
            background-color: pink;
        }
    </style>
</head>
<body>
    <div>classList添加</div>
    <div class="second">classList删除</div>
    <div class="one two three"></div>

    <script>
        var divs = document.querySelectorAll('div');
        divs[0].addEventListener('click', function() {
            // 添加类名， 是在后面追加类名不会覆盖以前的类名，注意前面不需要加。
            this.classList.add('first');
        });
        divs[1].addEventListener('click', function() {
            this.classList.remove('second');
        });

        divs[2].addEventListener('click', function() {
            // 切换元素方法， 如果有则删除， 没有则添加
            this.classList.toggle('one');
            this.classList.toggle('four');
        });
    </script>
</body>
</html>